//1110160001
//1110160017
function modify_info(res) {
    const name = document.getElementById("name");
    const phone = document.getElementById("phone");
    const photo = document.getElementById("photo");
    const i_name = document.getElementById("i-name");
    const i_code = document.getElementById("i-code");
    const i_type = document.getElementById("i-type");
    const i_sex = document.getElementById("i-sex");
    const i_college = document.getElementById("i-college");
    const i_square = document.getElementById("i-square");

    i_name.innerText = name.innerText = res["name"];
    phone.innerText = res["telephone"];
    photo.style.setProperty("background-image", "url('" + res["photo"] + "')");
    document.styleSheets[1].insertRule(".personal-pic:after{background-image: url(\"" + res["photo"] + "\");}", document.styleSheets[1].cssRules.length);
    i_code.innerText = res["teacher_code"];
    i_type.innerText = res["teacher_type"];
    i_sex.innerText = res["sex"];
    i_college.innerText = res["college"];
    i_square.innerText = res["square"];
}


function modify_leave(token) {
    httpRequest("http://172.27.71.125:7402/leave/api/getexaminingleavings", JSON.stringify({"token": token}),
        response => {
            const res = JSON.parse(response)["result"];
            const tbody = document.getElementById("leave-table").children[1];
            const t_length = tbody.rows.length;
            for (let i = 0; i < t_length; i++) {
                tbody.deleteRow(0);
            }
            for (let i = 0; i < res.length; i++) {
                tbody.insertRow(i);
                for (let j = 0; j < 5; j++) {
                    tbody.rows[i].insertCell(j);
                    tbody.rows[i].cells[j].style = "vertical-align: middle;";
                }
                tbody.rows[i].cells[0].innerText = res[i]["leaving_code"];
                tbody.rows[i].cells[1].innerText = res[i]["student_name"];
                tbody.rows[i].cells[2].innerText = res[i]["leaving_reason"];
                tbody.rows[i].cells[3].innerText = res[i]["leaving_date"];
                tbody.rows[i].cells[4].innerHTML = "<a class=\"btn btn-outline-primary\" href=\"leave-detail.html?leaving_id=" + res[i]["leaving_id"] + "&student_id=" + res[i]["account_id"] + "&token=" + sessionStorage.getItem("token") + "\" target=\"_blank\">查看详情</a>";
            }
        });
}

function modify_history(token) {
    httpRequest("http://172.27.71.125:7402/leave/api/getexaminehistory", JSON.stringify({"token": token}),
        response => {
        const res = JSON.parse(response)["result"];
        const tbody = document.getElementById("history-table").children[1];
        const t_length = tbody.rows.length;
        for (let i = 0; i < t_length; i++) {
            tbody.deleteRow(0);
        }
        for (let i = 0; i < res.length; i++) {
            tbody.insertRow(i);
            for (let j = 0; j < 6; j++) {
                tbody.rows[i].insertCell(j);
                tbody.rows[i].cells[j].style = "vertical-align: middle;";
            }
            tbody.rows[i].cells[0].innerText = res[i]["leavingCode"];
            tbody.rows[i].cells[1].innerText = res[i]["leavingReason"];
            tbody.rows[i].cells[2].innerText = res[i]["leavingDate"];
            tbody.rows[i].cells[3].innerText = res[i]["examineResult"];
            tbody.rows[i].cells[4].innerText = res[i]["examineDate"];
            tbody.rows[i].cells[5].innerHTML = "<a class=\"btn btn-outline-primary\" href=\"detail.html?leaving_id=" + res[i]["leavingId"] + "&token=" + sessionStorage.getItem("token") + "&identity=" + sessionStorage.getItem("identity") + "\" target=\"_blank\">查看详情</a>";
        }
        });
}

function fresh() {
    const token = sessionStorage.getItem("token");
    const t_logout = document.getElementById("logout");

    t_logout.onclick = logout;

    if (token == null || token === "") {
        urlReplace("login.html");
        return;
    }

    httpRequest("http://172.27.71.125:7402/leave/api/getteacher", JSON.stringify({"token": token}),
        response => {
            const res = JSON.parse(response)["result"];
            modify_info(res);
            modify_leave(token);
            modify_history(token);
        },
        _ => {
            urlReplace("login.html");
        });
}

window.onload = fresh;
window.onfocus = fresh;

